from main import *
from vtktest import vtkSpectrogram

windowsize = 44100 / 30
startingtime = 44100 * 0
#wav, fs, enc = wavread("/home/kkb110/music/research/ssnote.wav")
wav, fs, enc = wavread("/home/kkb110/music/research/pianog.wav")
xxxxx = rt(windowsize)
#data = xxxxx.fJ_generator.f([1, 0, 0, 0, 20 * pi * 2, 0]+[3,0,0,2,67*pi*2,0])
vtks = vtkSpectrogram()

t = linspace(0, 1, windowsize / 100, endpoint=False)
residual = array([])
for i in xrange(50):
    print wav.T.shape
    data = wav.T[0, startingtime + windowsize / 2 * i:startingtime + windowsize / 2 * i + windowsize]
    x = xxxxx.rt(data)
#    xx = x.reshape(-1, 6)
    xx = x.reshape(-1, 6)
    
    
#    plot(data)
#    plot(xxxxx.fJ_generator.f(x), 'r--')
#    plot(data - xxxxx.fJ_generator.f(x))
#    show()
    
    
    for xxx in xx:
#        plot(xxxxx.fJ_generator.f(xxx))
#        show()
#        vtks.addLine((t + i / 2.0) * 0.1,
#                    log(xxx[4] + xxx[5] * t ** 2) * 0.2,
#                    log(1 + exp(100 * (xxx[0] + xxx[1] * t + xxx[2] * t ** 2))) / 100 * 0.5,
#                    log(1 + exp(100 * (xxx[0] + xxx[1] * t + xxx[2] * t ** 2))) / 100 * 0.04,
#                    [127] * windowsize)
        vtks.addLine((t + i / 2.0) * 0.1,
                    log(xxx[4] + xxx[5] * t ** 2) * 0.2,
                    log(1 + exp(10 * (xxx[0] + xxx[1] * t + xxx[2]*t**2))) / 10 * 0.5,
                    log(1 + exp(10 * (xxx[0] + xxx[1] * t + xxx[2]*t**2 ))) / 10 * 0.04,
                    [127] * windowsize)
vtks.start()
while(True):
    i = input("1 data, 2 x, 3, data-x")
    if i == 1:
        play(data)
    if i == 2:
        play(xxxxx.fJ_generator.f(x))
    if i == 3:
        play(data - xxxxx.fJ_generator.f(x))

input("sdf")
